package com.buyi.easyhome.ui.view

import android.graphics.Typeface
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup.LayoutParams
import android.widget.TextClock
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFontFamilyResolver
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontSynthesis
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.viewinterop.AndroidView
import com.buyi.easyhome.R

@Composable
fun TimePage(modifier: Modifier = Modifier) {
    Column(
        modifier = modifier.fillMaxSize(),
        // on below line we are adding
        // horizontal alignment for our column
        horizontalAlignment = Alignment.CenterHorizontally,
        // on below line we are adding
        // vertical arrangement for our column
        verticalArrangement = Arrangement.Center
    ) {
        MyTextClock()
    }
}

@Composable
fun MyTextClock(
    style: TextStyle = MaterialTheme.typography.labelLarge,
) {
    val timeFormat = stringResource(R.string.clock_appwidget_text_clock_format)
    // 计算字体大小
    val textSizeVal =
        (LocalContext.current.resources.displayMetrics.widthPixels / 5).toFloat() * 1.5f

    val resolver = LocalFontFamilyResolver.current
    val face: Typeface = remember(resolver, style) {
        resolver.resolve(
            fontFamily = style.fontFamily,
            fontWeight = style.fontWeight ?: FontWeight.Normal,
            fontStyle = style.fontStyle ?: FontStyle.Normal,
            fontSynthesis = style.fontSynthesis ?: FontSynthesis.All
        )
    }.value as Typeface

    val textColor = MaterialTheme.colorScheme.onSurface.toArgb()
    AndroidView(
        // on below line we are initializing our text clock.
        factory = { context ->
            TextClock(context).apply {
                gravity = Gravity.CENTER
                layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
                // on below line we are setting 12 hour format.
                format12Hour?.let { this.format12Hour = timeFormat }
                format24Hour?.let { this.format24Hour = timeFormat }
                // on below line we are setting time zone.
                timeZone?.let { this.timeZone = it }
                // on below line we are setting text size.
                textSize.let { this.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeVal) }
                setTextColor(textColor)
                typeface = face
            }
        },
    )
}